import 'dart:ui';

import 'package:box2d_flame/box2d.dart';
import 'package:flame/box2d/box2d_component.dart';
import 'package:flame/box2d/viewport.dart';
import 'package:flame/palette.dart';

List<Wall> createBoundaries(Box2DComponent box) {
  final Viewport viewport = box.viewport;
  final Vector2 screenSize = Vector2(viewport.size.width, viewport.size.height);
  final Vector2 topLeft =
      Vector2(viewport.width * -1.38, viewport.height * 1.38);
  final Vector2 bottomRight =
      Vector2(viewport.width * -0.35, viewport.height * 0.37);
  final Vector2 topRight = Vector2(bottomRight.x, topLeft.y);
  final Vector2 bottomLeft = Vector2(topLeft.x, bottomRight.y);

//  final Viewport viewport = box.viewport;
//  final Vector2 screenSize = Vector2(viewport.width, viewport.height);
//  final Vector2 topLeft = (screenSize / 2) * -1;
//  final Vector2 bottomRight = screenSize / 2;
//  final Vector2 topRight = Vector2(bottomRight.x, topLeft.y);
//  final Vector2 bottomLeft = Vector2(topLeft.x, bottomRight.y);

  return [
    Wall(topLeft, topRight, box),
    Wall(topRight, bottomRight, box),
    Wall(bottomRight, bottomLeft, box),
    Wall(bottomLeft, topLeft, box),
  ];
}

class Wall extends BodyComponent {
  Paint paint = BasicPalette.black.paint;
  final Vector2 start;
  final Vector2 end;

  Wall(this.start, this.end, Box2DComponent box) : super(box) {
    _createBody(start, end);
  }

  @override
  void renderPolygon(Canvas canvas, List<Offset> coordinates) {
    final start = coordinates[0];
    final end = coordinates[1];
    canvas.drawLine(start, end, paint);
  }

  void _createBody(Vector2 start, Vector2 end) {
    final PolygonShape shape = PolygonShape();
    shape.setAsEdge(start, end);

    final fixtureDef = FixtureDef()
      ..shape = shape
      ..restitution = 0.0
      ..friction = 0.1;

    final bodyDef = BodyDef()
      ..setUserData(this) // To be able to determine object in collision
      ..position = Vector2.zero()
      ..type = BodyType.STATIC;

    body = world.createBody(bodyDef)..createFixtureFromFixtureDef(fixtureDef);
  }
}
//
//class DemoLevel {
//  List<BodyComponent> _bodies = List();
//
//  DemoLevel(Box2DComponent box) {
//    _bodies.add(
//        WallBody(box, Orientation.portrait, 1.0, 0.05, Alignment.topCenter));
//    _bodies.add(
//        WallBody(box, Orientation.portrait, 1.0, 0.05, Alignment.bottomCenter));
//    _bodies.add(
//        WallBody(box, Orientation.portrait, 0.05, 1.0, Alignment.centerRight));
//    _bodies.add(
//        WallBody(box, Orientation.portrait, 0.05, 1.0, Alignment.centerLeft));
//  }
//
//  List<BodyComponent> get bodies => _bodies;
//}
//
//class WallBody extends BodyComponent {
//  Orientation orientation;
//  double widthPercent;
//  double heightPercent;
//  Alignment alignment;
//
//  bool first = true;
//
//  WallBody(Box2DComponent box, this.orientation, this.widthPercent,
//      this.heightPercent, this.alignment)
//      : super(box) {
//    _createBody();
//  }
//
//  void _createBody() {
//    double width = box.viewport.width * widthPercent;
//    double height = box.viewport.height * heightPercent;
//
//    double x = alignment.x * (box.viewport.width - width);
//    double y = (-alignment.y) * (box.viewport.height - height);
//
//    final shape = PolygonShape();
//    shape.setAsBoxXY(width / 2, height / 2);
//    final fixtureDef = FixtureDef();
//    fixtureDef.shape = shape;
//
//    fixtureDef.restitution = 0.0;
//    fixtureDef.friction = 0.2;
//    final bodyDef = BodyDef();
//    bodyDef.position = Vector2(x / 2, y / 2);
//    Body groundBody = world.createBody(bodyDef);
//    groundBody.createFixtureFromFixtureDef(fixtureDef);
//    this.body = groundBody;
//  }
//}
